package cn.bornsoft.mobileServer.db;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * BcNews entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.bornsoft.mobileServer.db.BcNews
 * @author MyEclipse Persistence Tools
 */

public class BcNewsDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(BcNewsDAO.class);
	// property constants
	public static final String CLASSID = "classid";
	public static final String TITLE = "title";
	public static final String SUBTITLE = "subtitle";
	public static final String TITLESTYLE = "titlestyle";
	public static final String NEWSTYPE = "newstype";
	public static final String HEADNEWSPATH = "headnewspath";
	public static final String CONTENT = "content";
	public static final String FILENAME = "filename";
	public static final String FILEEXTNAME = "fileextname";
	public static final String PATH = "path";
	public static final String KEYWORDS = "keywords";
	public static final String TXTSOURCE = "txtsource";
	public static final String AUTHOR = "author";
	public static final String EDITER = "editer";
	public static final String CLICKNUM = "clicknum";
	public static final String RECTF = "rectf";
	public static final String PICPATH = "picpath";
	public static final String NAVIWORDS = "naviwords";
	public static final String AUDITTF = "audittf";
	public static final String DELTF = "deltf";
	public static final String BROWPOP = "browpop";
	public static final String SHOWREVIEWTF = "showreviewtf";
	public static final String REVIEWTF = "reviewtf";
	public static final String SBSNEWS = "sbsnews";
	public static final String MARQUEENEWS = "marqueenews";
	public static final String PROCLAIMNEWS = "proclaimnews";
	public static final String FILTERNEWS = "filternews";
	public static final String NEWSTEMPLET = "newstemplet";
	public static final String FOCUSNEWSTF = "focusnewstf";
	public static final String CLASSICALNEWSTF = "classicalnewstf";
	public static final String TODAYNEWSTF = "todaynewstf";
	public static final String SPECIALID = "specialid";
	public static final String TOPNEWSTF = "topnewstf";
	public static final String CREATEMAN = "createman";
	public static final String USERID = "userid";
	public static final String AUDITMAN = "auditman";
	public static final String AUDITMANDID = "auditmandid";
	public static final String CREATEMANDEPART = "createmandepart";
	public static final String REFRESHTF = "refreshtf";
	public static final String SAVETF = "savetf";
	public static final String ISTOP = "istop";

	public void save(BcNews transientInstance) {
		log.debug("saving BcNews instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(BcNews persistentInstance) {
		log.debug("deleting BcNews instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public BcNews findById(java.lang.String id) {
		log.debug("getting BcNews instance with id: " + id);
		try {
			getSession().clear();// 清除查询缓存

			BcNews instance = (BcNews) getSession().get(
					"cn.bornsoft.mobileServer.db.BcNews", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(BcNews instance) {
		log.debug("finding BcNews instance by example");
		try {
			List results = getSession().createCriteria(
					"cn.bornsoft.mobileServer.db.BcNews").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding BcNews instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from BcNews as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByClassid(Object classid) {
		return findByProperty(CLASSID, classid);
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findBySubtitle(Object subtitle) {
		return findByProperty(SUBTITLE, subtitle);
	}

	public List findByTitlestyle(Object titlestyle) {
		return findByProperty(TITLESTYLE, titlestyle);
	}

	public List findByNewstype(Object newstype) {
		return findByProperty(NEWSTYPE, newstype);
	}

	public List findByHeadnewspath(Object headnewspath) {
		return findByProperty(HEADNEWSPATH, headnewspath);
	}

	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List findByFilename(Object filename) {
		return findByProperty(FILENAME, filename);
	}

	public List findByFileextname(Object fileextname) {
		return findByProperty(FILEEXTNAME, fileextname);
	}

	public List findByPath(Object path) {
		return findByProperty(PATH, path);
	}

	public List findByKeywords(Object keywords) {
		return findByProperty(KEYWORDS, keywords);
	}

	public List findByTxtsource(Object txtsource) {
		return findByProperty(TXTSOURCE, txtsource);
	}

	public List findByAuthor(Object author) {
		return findByProperty(AUTHOR, author);
	}

	public List findByEditer(Object editer) {
		return findByProperty(EDITER, editer);
	}

	public List findByClicknum(Object clicknum) {
		return findByProperty(CLICKNUM, clicknum);
	}

	public List findByRectf(Object rectf) {
		return findByProperty(RECTF, rectf);
	}

	public List findByPicpath(Object picpath) {
		return findByProperty(PICPATH, picpath);
	}

	public List findByNaviwords(Object naviwords) {
		return findByProperty(NAVIWORDS, naviwords);
	}

	public List findByAudittf(Object audittf) {
		return findByProperty(AUDITTF, audittf);
	}

	public List findByDeltf(Object deltf) {
		return findByProperty(DELTF, deltf);
	}

	public List findByBrowpop(Object browpop) {
		return findByProperty(BROWPOP, browpop);
	}

	public List findByShowreviewtf(Object showreviewtf) {
		return findByProperty(SHOWREVIEWTF, showreviewtf);
	}

	public List findByReviewtf(Object reviewtf) {
		return findByProperty(REVIEWTF, reviewtf);
	}

	public List findBySbsnews(Object sbsnews) {
		return findByProperty(SBSNEWS, sbsnews);
	}

	public List findByMarqueenews(Object marqueenews) {
		return findByProperty(MARQUEENEWS, marqueenews);
	}

	public List findByProclaimnews(Object proclaimnews) {
		return findByProperty(PROCLAIMNEWS, proclaimnews);
	}

	public List findByFilternews(Object filternews) {
		return findByProperty(FILTERNEWS, filternews);
	}

	public List findByNewstemplet(Object newstemplet) {
		return findByProperty(NEWSTEMPLET, newstemplet);
	}

	public List findByFocusnewstf(Object focusnewstf) {
		return findByProperty(FOCUSNEWSTF, focusnewstf);
	}

	public List findByClassicalnewstf(Object classicalnewstf) {
		return findByProperty(CLASSICALNEWSTF, classicalnewstf);
	}

	public List findByTodaynewstf(Object todaynewstf) {
		return findByProperty(TODAYNEWSTF, todaynewstf);
	}

	public List findBySpecialid(Object specialid) {
		return findByProperty(SPECIALID, specialid);
	}

	public List findByTopnewstf(Object topnewstf) {
		return findByProperty(TOPNEWSTF, topnewstf);
	}

	public List findByCreateman(Object createman) {
		return findByProperty(CREATEMAN, createman);
	}

	public List findByUserid(Object userid) {
		return findByProperty(USERID, userid);
	}

	public List findByAuditman(Object auditman) {
		return findByProperty(AUDITMAN, auditman);
	}

	public List findByAuditmandid(Object auditmandid) {
		return findByProperty(AUDITMANDID, auditmandid);
	}

	public List findByCreatemandepart(Object createmandepart) {
		return findByProperty(CREATEMANDEPART, createmandepart);
	}

	public List findByRefreshtf(Object refreshtf) {
		return findByProperty(REFRESHTF, refreshtf);
	}

	public List findBySavetf(Object savetf) {
		return findByProperty(SAVETF, savetf);
	}

	public List findByIstop(Object istop) {
		return findByProperty(ISTOP, istop);
	}

	/**
	 * 测试，取出内容list
	 * 
	 * @return
	 */
	public List findAll() {
		log.debug("finding all BcNews instances");
		try {
			getSession().clear();// 清除查询缓存
			String queryString = "from BcNews t where t.picpath  is not null and t.refreshtf=1 and t.classid='8aac82101fdead47011fe40feaf70951'";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setMaxResults(5);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/**
	 * 根据一个或多个栏目ID，取max条新闻
	 * 
	 * @param num
	 * @param classid
	 * @return
	 */
	public List findByClassID(int max, int page, String classid) {
		log.debug("finding all BcNews instances");
		String[] classids = classid.split(",");// 所有的栏目以(,)逗号隔开
		ArrayList<String> classidlist = new ArrayList<String>(Arrays
				.asList(classids));
		try {
			getSession().clear();// 清除查询缓存
			String queryString = "from BcNews t where t.classid in (:classidList) and t.refreshtf=1  order by t.adddate desc";

			Query queryObject = getSession().createQuery(queryString);
			if (max > 100) {// 防止取太多
				max = 100;
			}
			queryObject.setParameterList("classidList", classidlist);
			queryObject.setFirstResult(max * (page-1));//
			queryObject.setMaxResults(max);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	
	public List findByNewsids(int max, int page, String newsids) {
		log.debug("finding all BcNews instances");
		String[] classids = newsids.split(",");// 所有以(|)逗号隔开
		ArrayList<String> classidlist = new ArrayList<String>(Arrays
				.asList(classids));
		try {
			getSession().clear();// 清除查询缓存
			String queryString = "from BcNews t where t.newsid in (:classidList) and t.refreshtf=1  order by t.adddate desc";

			Query queryObject = getSession().createQuery(queryString);
			if (max > 100) {// 防止取太多
				max = 100;
			}
			queryObject.setParameterList("classidList", classidlist);
			queryObject.setFirstResult(max * (page-1));//
			queryObject.setMaxResults(max);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	/**
	 * 合并记录
	 * 
	 * @param detachedInstance
	 * @return
	 */
	public BcNews merge(BcNews detachedInstance) {
		log.debug("merging BcNews instance");
		try {
			BcNews result = (BcNews) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(BcNews instance) {
		log.debug("attaching dirty BcNews instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(BcNews instance) {
		log.debug("attaching clean BcNews instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}